package dailytools.dailyui.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventManager {
    static Map<String, List<Runnable>> eventListenerListMap = new HashMap<>();

    public static void registerListener(String eventType, Runnable listener) {
        List<Runnable> eventListenerList = eventListenerListMap.getOrDefault(eventType, new ArrayList<Runnable>());
        eventListenerList.add(listener);
        eventListenerListMap.put(eventType, eventListenerList);
    }

    public static void publishEvent(String eventType) {
        eventListenerListMap.getOrDefault(eventType, new ArrayList<>()).forEach(it -> {
            it.run();
        });
    }

    public static void unregisterListener(String eventType, Runnable listener) {
        List<Runnable> eventListenerList = eventListenerListMap.getOrDefault(eventType, new ArrayList<Runnable>());
        eventListenerList.remove(listener);
    }
}
